<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['basic']=$this->plugin_setting('basic');
$return['banner']=$return['basic']['hdps'];
if($_RQ['op']=='main'){
	/*******main*******/
	$id=$_RQ['id'];
	if($id){
		$order=pdo_get('jy_tour_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}else{
		$_RQ['buynum']=$_RQ['buynum']?:1;
		$order=$_RQ;
	}
	$way=pdo_get('jy_tour_way',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['way_id']));
	$order['way']=$way;
	if($way){
		$images=json_decode($way['images'],true);
		$merch=pdo_get('jy_tour_merch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$way['merchid']));
		$way['merch']=$merch;
		$way['images']=$images;
	}
	$return['way']=$way;
	$return['post']=$order;
	$return['post']['total_price']=$return['post']['price']*$return['post']['buynum'];
	$return['post']['diyfields']=$return['basic']['diyfields'];
	exi($return);
}
//提交
if($_RQ['op']=='submit'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$post){
		exi('请完善相关信息！','error');
	}
	if(!$post['name']){
		exi('请选择联系人姓名！','error');
	}
	if(!$post['mobile']){
		exi('请填写联系电话！','error');
	}
	if(!$post['shencode']){
		//exi('请填写身份证号！','error');
	}
	//生成订单
	$update=array(
		'buynum'=>$post['buynum']?:1,
		'price'=>$post['price'],
		'fee'=>$post['total_price'],
		'name'=>$post['name'],
		'mobile'=>$post['mobile'],
		'remark'=>trim($post['remark']),
		//'shencode'=>trim($post['shencode']),
		'diyfields'=>ijson_encode($post['diyfields']),
	);
	if($id){
		pdo_update('jy_tour_order',$update,array('id'=>$id));
	}else{
		$update['merchid']=$post['merchid'];
		$update['spot_id']=$post['spot_id'];
		$update['ticket_id']=$post['ticket_id'];
		$update['suitid']=$post['suitid'];
		$update['go_time']=$post['go_time'];
		$update['uniacid']=$_SESSION['uniacid'];
		$update['mid']=$_SESSION['uid'];
		$update['ordersn']=get_ordersn($this->tablename('order'),'ordersn');
		$update['createtime']=TIMESTAMP;
		pdo_insert('jy_tour_order',$update);
		$id=pdo_insertid();
	}
	$params['fee']=$post['total_price'];
	$params['title']='旅游订单';
	$params['id']=$id;
	$params['plugin']='jy_tour';
	$params['back_url']='jy_tour/spot.over_book/payresult';
	$pay_id=cfc('pay')->create_pay_order($params);
	$this->pdo_update('order',array('pay_id'=>$pay_id),array('id'=>$id));
	$res=array('pay_id'=>$pay_id,'order_id'=>$id);
	exi($res);
	
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_tour_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_tour_order',array('status'=>'1','fee'=>$payresult['fee'],'paytype'=>$payresult['pay_type'],'paytime'=>TIMESTAMP,'transid'=>$payresult['transaction_id']),array('id'=>$order['id']));
	exi('支付成功','success','jy_tour/order.list//status=1&id='.$order['id']);
}
